package com.jshx.sz.gdjt.thread;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;

import com.jshx.sz.gdjt.util.TDESEncodeUtils;
import com.jshx.sz.gdjt.constant.ApplicationGlobal;

import android.content.pm.PackageInfo;
import android.os.Handler;
import android.os.Message;
 

public class CopyOfLoginThread  extends Thread{
	private Handler handler;
	private String userName;
	private String password;
	private String responseMsg;
	private HashMap<String, String> returnMap = new HashMap<String, String>();
	
	public CopyOfLoginThread()
	{
		
	}
	
	public CopyOfLoginThread(Handler handler, String userName, String password, HashMap<String, String> returnMap)
	{
		this.handler=handler;
		this.userName=userName;
		this.password=password;
		this.returnMap=returnMap;
	}
	public void run() 
	{
		try {
				checkLogin() ;
		} catch (Exception e) {
			System.out.println(e);
			e.printStackTrace();
			Message msg=new Message();
			msg.what=ApplicationGlobal.loginfail;
			handler.sendMessage(msg) ;
		}  
	}
	
	
	private void checkLogin() throws Exception
	{
		String detailUrl=ApplicationGlobal.serviceUrl+"strTemp=%7B'classname':'GdjtReq','actionName':'login','loginName':'"+this.userName+"','password':'"+this.password+"'%7D";
		//detailUrl=ApplicationGlobal.serviceUrl;
		StringBuffer sb = new StringBuffer();
		
		HttpClient client = new DefaultHttpClient();
		HttpParams params = client.getParams();
		HttpConnectionParams.setConnectionTimeout(params, 20000);
		HttpConnectionParams.setSoTimeout(params, 20000);
		HttpPost post = new HttpPost(detailUrl);
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
	  
//		
//		nameValuePairs.add(new BasicNameValuePair("loginName",TDESEncodeUtils .encrypt( this.userName)));
//		nameValuePairs.add(new BasicNameValuePair("password", TDESEncodeUtils .encrypt( this.password) ));
//		 
//		post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
		HttpResponse response = client.execute(post);
		Header header = response.getFirstHeader("exception");
		if (header != null) {
			responseMsg = header.getValue();
		} else {
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(entity.getContent(), "UTF-8"),
						8192);

				String line = null;
				while ((line = reader.readLine()) != null) {
					sb.append(line + "\n");
				}
				reader.close();
			}
			
			if (sb.length() > 0) {
				String result = sb.toString();
				if (result != null && !result.trim().equals("")) {
					JSONObject jo = new JSONObject(result);
					
					JSONObject obj = new JSONObject(jo.getString("content"));
								
				    if(jo.has("className")) {
						returnMap.put("className", jo.getString("className"));
					} else {
						returnMap.put("className", "");
					}
					if(jo.has("code")) {
						returnMap.put("code", jo.getString("code"));
					} else {
						returnMap.put("code", "");
					}
					if(jo.has("content")) {
						returnMap.put("content", jo.getString("content"));
					} else {
						returnMap.put("content", "");
					} 
					if(jo.has("id")) {
						returnMap.put("id", jo.getString("id"));
					} else {
						returnMap.put("id", "");
					} 
					if(jo.has("message")) {
						returnMap.put("message", jo.getString("message"));
					} else {
						returnMap.put("message", "");
					} 
					if(jo.has("page")) {
						returnMap.put("page", jo.getString("page"));
					} else {
						returnMap.put("page", "");
					} 
					if(jo.has("total")) {
						returnMap.put("total", jo.getString("total"));
					} else {
						returnMap.put("total", "");
					} 
					
					
					if(obj.has("loginName")) {
						returnMap.put("loginName", obj.getString("loginName"));
					} else {
						returnMap.put("loginName", "");
					} 
					if(obj.has("userName")) {
						returnMap.put("userName", obj.getString("userName"));
					} else {
						returnMap.put("userName", "");
					} 
					if(obj.has("userId")) {
						returnMap.put("userId", obj.getString("userId"));
					} else {
						returnMap.put("userId", "");
					} 
					if(obj.has("priCheck")) {
						returnMap.put("priCheck", obj.getString("priCheck"));
					} else {
						returnMap.put("priCheck", "");
					} 
					if(obj.has("priExamine")) {
						returnMap.put("priExamine", obj.getString("priExamine"));
					} else {
						returnMap.put("priExamine", "");
					} 
					if(obj.has("content")) {
						returnMap.put("content", obj.getString("content"));
					} else {
						returnMap.put("content", "");
					} 
					 
					Message msg=new Message();
					msg.what=ApplicationGlobal.loginsucc;
					handler.sendMessage(msg) ;
					return;
				}
			}
		}
		Message msg=new Message();
		msg.what=ApplicationGlobal.loginfail;
		handler.sendMessage(msg) ;
		return;
	}
}
